<?php

/**
 * This file is part of Switcher.
 * 
 * Switcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Switcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Switcher. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace switcher\controllers;

/**
 * This is the <i>Controller</i> part of the MVC implementation in the Switcher Framework. It's role
 * is to populate the context (MVC <i>Model</i> part) of a <b>Renderer</b> (MVC <i>View</i> part).
 * 
 * <p>This is the base class of all controllers. User-defined controllers must extend this class or
 * any subclass. It provides all the utility methods to manipulate the renderer's context. Unlike
 * <b>Renderer</b>, it doesn't declare a single endpoint for all controllers. A controller must
 * declare public no-arg methods as endpoints, this allow to group in the same class, actions related
 * to the same matter.</p>
 *
 * @author Dri <switcher.framework@gmail.com>
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 * @package switcher\controllers
 * @since 1.0.0
 */
abstract class Controller
{
	/**
	 * The renderer which context is populated.
	 * @var switcher\renderers\Renderer
	 */
	private $renderer;
	
	/**
	 * The name of the current view.
	 * @var string
	 */
	protected $view;
	
	/**
	 * The root path to the current application.
	 * @var string
	 */
	protected $root;
	
	/**
	 * The path information for the view. It is an associative array with the following keys :
	 * <ul>
	 *  <li>pattern (string) : the declared pattern</li>
	 *  <li>matches (array) : the parts matching regexp groups or shell wildcards</li>
	 *  <li>regexp (bool) : indicates whether the pattern is a regular expression</li>
	 * </ul>
	 * @var array
	 * @see switcher\utils\FileHelper::match()
	 */
	protected $path;
	
	/**
	 * Logger.
	 * @var switcher\log\Logger
	 */
	protected $logger;
	
	/**
	 * No-arg constructor.
	 */
	public final function __construct()
	{
	}
	
	protected function renderer()
	{
		return $this->renderer;
	}
	
	/**
	 * Delegate method for getting a context parameter.
	 * 
	 * @param string $name The name of the context parameter
	 * @return mixed The context parameter or null if it doesn't exist
	 * @throws RendererException If $name isn't a valid context parameter name
	 */
	public final function getContextParameter($name)
	{
		return $this->renderer->getContextParameter($name);
	}
	
	/**
	 * Delegate method for setting a context parameter.
	 * 
	 * @param string $name The name of the context parameter
	 * @throws RendererException If $name isn't a valid context parameter name
	 */
	public final function setContextParameter($name, $value)
	{
		$this->renderer->setContextParameter($name, $value);
	}
	
	/**
	 * Delegate method for setting context parameters.
	 * 
	 * @param array $values An associative array containing named values
	 * @throws RendererException If a value doesn't have a valid context parameter name
	 */
	public final function setContextParameters(array $values)
	{
		$this->renderer->setContextParameters($values);
	}
	
	/**
	 * Delegate method for removing a context parameter.
	 * 
	 * @param string $name The name of the context parameter
	 * @throws RendererException If $name isn't a valid context parameter name
	 */
	public final function removeContextParameter($name)
	{
		$this->renderer->removeContextParameter($name);
	}
	
	/**
	 * Return the view name associated to a status other than 200.
	 * 
	 * @param string $status The HTTP response status
	 * @return string The $status itself unless overrided
	 */
	public function getViewName($status)
	{
		return $status;
	}
}

?>